package com.github.iot.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatImageButton;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import com.github.iot.R;
import com.github.iot.entity.Door;

import java.util.List;

/**
 * 柜门适配器
 *
 * @Author: 梁声洪
 * @Date: 2019/12/24 22:47
 * @Version: v1.0.0
 */
public class DoorAdapter extends RecyclerView.Adapter<DoorAdapter.DoorViewHolder> implements View.OnTouchListener {

    private Context context;
    private List<Door> list;
    private View inflater;

    public DoorAdapter(Context context, List<Door> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public DoorViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        inflater = LayoutInflater.from(context).inflate(R.layout.door_item, parent, false);
        DoorViewHolder viewHolder = new DoorViewHolder(inflater);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(DoorViewHolder holder, int position) {
        Door door = list.get(position);
        holder.mName.setText(door.getName());

        if (door.getType().equals("柜门")) {
            holder.mOpen.setBackgroundResource(R.mipmap.red_open);
            holder.mClose.setBackgroundResource(R.mipmap.red_close);
        }

        if (door.getType().equals("灯箱")) {
            holder.mOpen.setBackgroundResource(R.mipmap.red_rise);
            holder.mClose.setBackgroundResource(R.mipmap.red_drop);
        }

        if (door.getType().equals("背板")) {
            holder.mOpen.setBackgroundResource(R.mipmap.red_push);
            holder.mClose.setBackgroundResource(R.mipmap.red_pull);
        }

        /*if (position % 2 == 1) {
            holder.mShowcaseLayout.setVisibility(View.GONE);
           *//* ViewGroup.LayoutParams layoutParams = holder.mShowcaseLayout1.getLayoutParams();
            layoutParams.height = 160;
            holder.mShowcaseLayout1.setLayoutParams(layoutParams);*//*
        } else {
            holder.mTypeName.setText(door.getTypeName());
        }*/
        holder.mShowcaseLayout.setVisibility(View.GONE);

        holder.mOpen.setTag(position);
        holder.mClose.setTag(position);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    /**
     * 声明自定义的接口
     */
    private OnItemClickListener mOnItemClickListener;

    /**
     * 定义方法并传给外面的使用者
     *
     * @param listener
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (null != mOnItemClickListener) {

            int position = (int) v.getTag();
            int action = event.getAction();
            Door door = list.get(position);

            switch (v.getId()) {
                case R.id.btn_close:
                    // 按下
                    if (action == MotionEvent.ACTION_DOWN) {
                        mOnItemClickListener.closeActionDownClick(v, door, position);
                    }

                    // 松开
                    if (action == MotionEvent.ACTION_UP) {
                        mOnItemClickListener.closeActionUpClick(v, door, position);
                    }
                    break;
                case R.id.btn_open:
                    // 按下
                    if (action == MotionEvent.ACTION_DOWN) {
                        mOnItemClickListener.openActionDownClick(v, door, position);
                    }

                    // 松开
                    if (action == MotionEvent.ACTION_UP) {
                        mOnItemClickListener.openActionUpClick(v, door, position);
                    }
                    break;
                default:

                    break;
            }
        }
        return false;
    }

    class DoorViewHolder extends RecyclerView.ViewHolder {
        LinearLayout mShowcaseLayout;
        LinearLayout mShowcaseLayout1;
        AppCompatTextView mName;
        TextView mTypeName;
        AppCompatImageButton mOpen;
        AppCompatImageButton mClose;

        public DoorViewHolder(View itemView) {
            super(itemView);
            mName = itemView.findViewById(R.id.tv_door_name);
            mShowcaseLayout = itemView.findViewById(R.id.showcase_layout);
            mShowcaseLayout1 = itemView.findViewById(R.id.showcase_layout1);
            mTypeName = itemView.findViewById(R.id.tv_type_name);
            mOpen = itemView.findViewById(R.id.btn_open);
            mClose = itemView.findViewById(R.id.btn_close);

            mOpen.setOnTouchListener(DoorAdapter.this);
            mClose.setOnTouchListener(DoorAdapter.this);
        }
    }

    /**
     * 自定义一个回调接口来实现Click和LongClick事件
     */
    public interface OnItemClickListener {

        /**
         * 关闭按钮按下事件
         *
         * @param v
         * @param door
         * @param position
         */
        void closeActionDownClick(View v, Door door, int position);

        /**
         * 关闭按钮松开事件
         *
         * @param v
         * @param door
         * @param position
         */
        void closeActionUpClick(View v, Door door, int position);

        /**
         * 打开按钮按下事件
         *
         * @param v
         * @param door
         * @param position
         */
        void openActionDownClick(View v, Door door, int position);

        /**
         * 打开按钮松开事件
         *
         * @param v
         * @param door
         * @param position
         */
        void openActionUpClick(View v, Door door, int position);
    }


}